//配置文件

//请求地址
var ApiURL = 'http://localhost:83/';
//document.write('<script src="/Data/Data.js" type="text/javascript"></script>');

/**
 * 公共方法
 */
var MyPublic = {
    
    /****读取URL参数*****/
    getUrlParam: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return decodeURI(r[2]); return null; //返回参数值
    },
    /******时间年月日时分秒格式化******/
    getFnTime: function (time) {
        if (time != null) {
            var date = new Date(time);
            var minute = date.getMinutes();
           
            if (minute < 10) {
                minute = '0' + minute;
            }
            return date.getFullYear() + '年' + (date.getMonth() + 1) + '月' + date.getDate() + '日  ' + date.getHours() + ':' + minute;
        }
        else {
            return '';
        }
    },
    /******时间年月日格式化******/
    getFnDate: function (time) {
        if (time != null) {
        	
            var date = new Date(time);
            return date.getFullYear() + '.' + (date.getMonth() + 1) + '.' + date.getDate();
        }
        else {
            return '';
        }
    },
    /******时间年月日格式化******/
    getFnChinaDate: function (time) {
        if (time != null) {
            var date = new Date(time);
            return date.getFullYear() + '年' + (date.getMonth() + 1) + '月' + date.getDate()+'日';
        }
        else {
            return '';
        }
    },
    /***********数据省略...**************/
    getFnDataellipsis:function(data){
    	data=data.replace(/\n/g,'');
    	data=data.replace(/=begin=/g,'').replace(/=end=/g,'').replace(/##/g,'');
    	if(data.length>40){
    		data=data.substring(0,40)+'...';
    	}
    	return data;
    }
}
/**
 * 封装ajaxpost函数
 * @param {any} opt
 */
function ajaxpost(opt) {
    opt = opt || {};
    opt.url = opt.url || '';
    opt.data = opt.data || null;
    opt.success = opt.success || function () { };
    layui.use('layer', function () {
        var layer = layui.layer;

        $.ajax({
            url: ApiURL + opt.url,
            type: 'post',
            contentType: 'application/json',
            data: JSON.stringify(opt.data),
            success: function (data) {
                opt.success(data);
            },
            error: function (XMLHttpRequest) {
                var state = XMLHttpRequest.status;
                if (state === 401) {
                    layer.confirm('登陆凭证已过期，请重新登陆！', { icon: 2, title: '警告' }, function (index) {

                        window.open("/login.html");
                        localStorage.clear();//清空存储的值
                        sessionStorage.clear();
                        layer.close(index);
                    });
                }
                else {
                    location.href = "error.html"
                }

            }
        })
    });   
    
}
/**
 * 封装ajaxget函数
 * @param {any} opt
 */
function ajaxget(opt) {
    opt = opt || {};
    opt.url = opt.url || '';
    opt.success = opt.success || function () { };
    layui.use('layer', function () {
        var layer = layui.layer;

        $.ajax({
            url: ApiURL + opt.url,
            type: 'get',
            contentType: 'application/json',
            success: function (data) {
                opt.success(data);
            },
            error: function (XMLHttpRequest) {
                var state = XMLHttpRequest.status;
                if (state === 401) {
                    layer.confirm('登陆凭证已过期，请重新登陆！', { icon: 2, title: '警告' }, function (index) {

                        window.open("/login.html");
                        localStorage.clear();//清空存储的值
                        sessionStorage.clear();
                        layer.close(index);
                    });
                }
                else {
                    location.href = "error.html"
                }

            }
        })
    });   
}
